home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / content / nsContentUtils.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  27KB  |  734 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code, released
  17.  * March 31, 1998.
  18.  *
  19.  * The Initial Developer of the Original Code is
  20.  * Netscape Communications Corporation.
  21.  * Portions created by the Initial Developer are Copyright (C) 1998
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /* A namespace class for static content utilities. */
  41.  
  42. #ifndef nsContentUtils_h___
  43. #define nsContentUtils_h___
  44.  
  45. #include "jspubtd.h"
  46. #include "nsAString.h"
  47. #include "nsIDOMScriptObjectFactory.h"
  48. #include "nsIJSContextStack.h"
  49. #include "nsIScriptContext.h"
  50. #include "nsCOMArray.h"
  51. #include "nsIStatefulFrame.h"
  52. #include "nsIPref.h"
  53. #include "nsINodeInfo.h"
  54. #include "nsNodeInfoManager.h"
  55. #include "nsContentList.h"
  56. #include "nsVoidArray.h"
  57.  
  58. class nsIXPConnect;
  59. class nsIContent;
  60. class nsIDOMNode;
  61. class nsIDocument;
  62. class nsIDocShell;
  63. class nsINameSpaceManager;
  64. class nsIScriptSecurityManager;
  65. class nsIThreadJSContextStack;
  66. class nsIParserService;
  67. class nsIIOService;
  68. class nsIURI;
  69. class imgIDecoderObserver;
  70. class imgIRequest;
  71. class imgILoader;
  72. class nsIPrefBranch;
  73. class nsIImage;
  74. class nsIImageLoadingContent;
  75. class nsIDOMHTMLFormElement;
  76. class nsIDOMDocument;
  77. class nsIConsoleService;
  78. class nsIStringBundleService;
  79. class nsIStringBundle;
  80. class nsIJSRuntimeService;
  81. struct JSRuntime;
  82. #ifdef MOZ_XTF
  83. class nsIXTFService;
  84. #endif
  85.  
  86. class nsContentUtils
  87. {
  88. public:
  89.   static nsresult Init();
  90.  
  91.   // You MUST pass the old ownerDocument of aContent in as aOldDocument and the
  92.   // new one as aNewDocument.  aNewParent is allowed to be null; in that case
  93.   // aNewDocument will be assumed to be the parent.  Note that at this point
  94.   // the actual ownerDocument of aContent may not yet be aNewDocument.
  95.   // XXXbz but then if it gets wrapped after we do this call but before its
  96.   // ownerDocument actually changes, things will break...
  97.   static nsresult ReparentContentWrapper(nsIContent *aContent,
  98.                                          nsIContent *aNewParent,
  99.                                          nsIDocument *aNewDocument,
  100.                                          nsIDocument *aOldDocument);
  101.  
  102.   static PRBool   IsCallerChrome();
  103.  
  104.   /*
  105.    * Returns true if the nodes are both in the same document or
  106.    * if neither is in a document.
  107.    * Returns false if the nodes are not in the same document.
  108.    */
  109.   static PRBool   InSameDoc(nsIDOMNode *aNode,
  110.                             nsIDOMNode *aOther);
  111.  
  112.   /**
  113.    * Do not ever pass null pointers to this method.  If one of your
  114.    * nsIContents is null, you have to decide for yourself what
  115.    * "IsDescendantOf" really means.
  116.    *
  117.    * @param  aPossibleDescendant node to test for being a descendant of
  118.    *         aPossibleAncestor
  119.    * @param  aPossibleAncestor node to test for being an ancestor of
  120.    *         aPossibleDescendant
  121.    * @return PR_TRUE if aPossibleDescendant is a descendant of
  122.    *         aPossibleAncestor (or is aPossibleAncestor).  PR_FALSE
  123.    *         otherwise.
  124.    */
  125.   static PRBool ContentIsDescendantOf(nsIContent* aPossibleDescendant,
  126.                                       nsIContent* aPossibleAncestor);
  127.  
  128.   /*
  129.    * This method fills the |aArray| with all ancestor nodes of |aNode|
  130.    * including |aNode| at the zero index.
  131.    *
  132.    * These elements were |nsIDOMNode*|s before casting to |void*| and must
  133.    * be cast back to |nsIDOMNode*| on usage, or bad things will happen.
  134.    */
  135.   static nsresult GetAncestors(nsIDOMNode* aNode,
  136.                                nsVoidArray* aArray);
  137.  
  138.   /*
  139.    * This method fills |aAncestorNodes| with all ancestor nodes of |aNode|
  140.    * including |aNode| (QI'd to nsIContent) at the zero index.
  141.    * For each ancestor, there is a corresponding element in |aAncestorOffsets|
  142.    * which is the IndexOf the child in relation to its parent.
  143.    *
  144.    * The elements of |aAncestorNodes| were |nsIContent*|s before casting to
  145.    * |void*| and must be cast back to |nsIContent*| on usage, or bad things
  146.    * will happen.
  147.    *
  148.    * This method just sucks.
  149.    */
  150.   static nsresult GetAncestorsAndOffsets(nsIDOMNode* aNode,
  151.                                          PRInt32 aOffset,
  152.                                          nsVoidArray* aAncestorNodes,
  153.                                          nsVoidArray* aAncestorOffsets);
  154.  
  155.   /*
  156.    * The out parameter, |aCommonAncestor| will be the closest node, if any,
  157.    * to both |aNode| and |aOther| which is also an ancestor of each.
  158.    */
  159.   static nsresult GetCommonAncestor(nsIDOMNode *aNode,
  160.                                     nsIDOMNode *aOther,
  161.                                     nsIDOMNode** aCommonAncestor);
  162.  
  163.   /*
  164.    * |aDifferentNodes| will contain up to 3 elements.
  165.    * The first, if present, is the common ancestor of |aNode| and |aOther|.
  166.    * The second, if present, is the ancestor node of |aNode| which is
  167.    * closest to the common ancestor, but not an ancestor of |aOther|.
  168.    * The third, if present, is the ancestor node of |aOther| which is
  169.    * closest to the common ancestor, but not an ancestor of |aNode|.
  170.    *
  171.    * @throws NS_ERROR_FAILURE if aNode and aOther are disconnected.
  172.    */
  173.   static nsresult GetFirstDifferentAncestors(nsIDOMNode *aNode,
  174.                                              nsIDOMNode *aOther,
  175.                                              nsCOMArray<nsIDOMNode>& aDifferentNodes);
  176.  
  177.   /**
  178.    * Compares the document position of nodes which may have parents.
  179.    * DO NOT pass in nodes that cannot have a parentNode. In other words:
  180.    * DO NOT pass in Attr, Document, DocumentFragment, Entity, or Notation!
  181.    * The results will be completely wrong!
  182.    *
  183.    * @param   aNode   The node to which you are comparing.
  184.    * @param   aOther  The reference node to which aNode is compared.
  185.    *
  186.    * @return  The document position flags of the nodes.
  187.    *
  188.    * @see nsIDOMNode
  189.    * @see nsIDOM3Node
  190.    */
  191.   static PRUint16 ComparePositionWithAncestors(nsIDOMNode *aNode,
  192.                                                nsIDOMNode *aOther);
  193.  
  194.   /**
  195.    * Reverses the document position flags passed in.
  196.    *
  197.    * @param   aDocumentPosition   The document position flags to be reversed.
  198.    *
  199.    * @return  The reversed document position flags.
  200.    *
  201.    * @see nsIDOMNode
  202.    * @see nsIDOM3Node
  203.    */
  204.   static PRUint16 ReverseDocumentPosition(PRUint16 aDocumentPosition);
  205.  
  206.   static PRUint32 CopyNewlineNormalizedUnicodeTo(const nsAString& aSource,
  207.                                                  PRUint32 aSrcOffset,
  208.                                                  PRUnichar* aDest,
  209.                                                  PRUint32 aLength,
  210.                                                  PRBool& aLastCharCR);
  211.  
  212.   static PRUint32 CopyNewlineNormalizedUnicodeTo(nsReadingIterator<PRUnichar>& aSrcStart, const nsReadingIterator<PRUnichar>& aSrcEnd, nsAString& aDest);
  213.  
  214.   static nsISupports *
  215.   GetClassInfoInstance(nsDOMClassInfoID aID);
  216.  
  217.   static const nsDependentSubstring TrimCharsInSet(const char* aSet,
  218.                                                    const nsAString& aValue);
  219.  
  220.   static const nsDependentSubstring TrimWhitespace(const nsAString& aStr,
  221.                                                    PRBool aTrimTrailing = PR_TRUE);
  222.  
  223.   static void Shutdown();
  224.  
  225.   /**
  226.    * Checks whether two nodes come from the same origin. aTrustedNode is
  227.    * considered 'safe' in that a user can operate on it and that it isn't
  228.    * a js-object that implements nsIDOMNode.
  229.    * Never call this function with the first node provided by script, it
  230.    * must always be known to be a 'real' node!
  231.    */
  232.   static nsresult CheckSameOrigin(nsIDOMNode* aTrustedNode,
  233.                                   nsIDOMNode* aUnTrustedNode);
  234.  
  235.   // Check if the (JS) caller can access aNode.
  236.   static PRBool CanCallerAccess(nsIDOMNode *aNode);
  237.  
  238.   /**
  239.    * Get the docshell through the JS context that's currently on the stack.
  240.    * If there's no JS context currently on the stack aDocShell will be null.
  241.    *
  242.    * @param aDocShell The docshell or null if no JS context
  243.    */
  244.   static nsIDocShell *GetDocShellFromCaller();
  245.  
  246.   /**
  247.    * Get the document through the JS context that's currently on the stack.
  248.    * If there's no JS context currently on the stack it will return null.
  249.    *
  250.    * @return The document or null if no JS context
  251.    */
  252.   static nsIDOMDocument *GetDocumentFromCaller();
  253.  
  254.   // Check if a node is in the document prolog, i.e. before the document
  255.   // element.
  256.   static PRBool InProlog(nsIDOMNode *aNode);
  257.  
  258.   static nsIParserService* GetParserServiceWeakRef();
  259.  
  260.   static nsINameSpaceManager* GetNSManagerWeakRef()
  261.   {
  262.     return sNameSpaceManager;
  263.   }
  264.  
  265.   static nsIIOService* GetIOServiceWeakRef()
  266.   {
  267.     return sIOService;
  268.   }
  269.  
  270.   static imgILoader* GetImgLoader()
  271.   {
  272.     return sImgLoader;
  273.   }
  274.  
  275. #ifdef MOZ_XTF
  276.   static nsIXTFService* GetXTFServiceWeakRef();
  277. #endif
  278.  
  279.   static nsIScriptSecurityManager* GetSecurityManager()
  280.   {
  281.     return sSecurityManager;
  282.   }
  283.  
  284.   static nsresult GenerateStateKey(nsIContent* aContent,
  285.                                    nsIDocument* aDocument,
  286.                                    nsIStatefulFrame::SpecialStateID aID,
  287.                                    nsACString& aKey);
  288.  
  289.   /**
  290.    * Create a new nsIURI from aSpec, using aBaseURI as the base.  The
  291.    * origin charset of the new nsIURI will be the document charset of
  292.    * aDocument.
  293.    */
  294.   static nsresult NewURIWithDocumentCharset(nsIURI** aResult,
  295.                                             const nsAString& aSpec,
  296.                                             nsIDocument* aDocument,
  297.                                             nsIURI* aBaseURI);
  298.  
  299.   /**
  300.    * Determine whether aContent is in some way associated with aForm.  If the
  301.    * form is a container the only elements that are considered to be associated
  302.    * with a form are the elements that are contained within the form. If the
  303.    * form is a leaf element then all elements will be accepted into this list,
  304.    * since this can happen due to content fixup when a form spans table rows or
  305.    * table cells.
  306.    */
  307.   static PRBool BelongsInForm(nsIDOMHTMLFormElement *aForm,
  308.                               nsIContent *aContent);
  309.  
  310.   static nsresult CheckQName(const nsAString& aQualifiedName,
  311.                              PRBool aNamespaceAware = PR_TRUE);
  312.  
  313.   static nsresult SplitQName(nsIContent* aNamespaceResolver,
  314.                              const nsAFlatString& aQName,
  315.                              PRInt32 *aNamespace, nsIAtom **aLocalName);
  316.  
  317.   static nsresult LookupNamespaceURI(nsIContent* aNamespaceResolver,
  318.                                      const nsAString& aNamespacePrefix,
  319.                                      nsAString& aNamespaceURI);
  320.  
  321.   static nsresult GetNodeInfoFromQName(const nsAString& aNamespaceURI,
  322.                                        const nsAString& aQualifiedName,
  323.                                        nsNodeInfoManager* aNodeInfoManager,
  324.                                        nsINodeInfo** aNodeInfo);
  325.  
  326.   static void SplitExpatName(const PRUnichar *aExpatName, nsIAtom **aPrefix,
  327.                              nsIAtom **aTagName, PRInt32 *aNameSpaceID);
  328.  
  329.   static nsAdoptingCString GetCharPref(const char *aPref);
  330.   static PRPackedBool GetBoolPref(const char *aPref,
  331.                                   PRBool aDefault = PR_FALSE);
  332.   static PRInt32 GetIntPref(const char *aPref, PRInt32 aDefault = 0);
  333.   static nsAdoptingString GetLocalizedStringPref(const char *aPref);
  334.   static nsAdoptingString GetStringPref(const char *aPref);
  335.   static void RegisterPrefCallback(const char *aPref,
  336.                                    PrefChangedFunc aCallback,
  337.                                    void * aClosure);
  338.   static void UnregisterPrefCallback(const char *aPref,
  339.                                      PrefChangedFunc aCallback,
  340.                                      void * aClosure);
  341.   static nsIPrefBranch *GetPrefBranch()
  342.   {
  343.     return sPrefBranch;
  344.   }
  345.  
  346.   static nsresult GetDocumentAndPrincipal(nsIDOMNode* aNode,
  347.                                           nsIDocument** aDocument,
  348.                                           nsIPrincipal** aPrincipal);
  349.  
  350.   /**
  351.    * Method to do security and content policy checks on the image URI
  352.    *
  353.    * @param aURI uri of the image to be loaded
  354.    * @param aContext the context the image is loaded in (eg an element)
  355.    * @param aLoadingDocument the document we belong to
  356.    * @param aImageBlockingStatus the nsIContentPolicy blocking status for this
  357.    *        image.  This will be set even if a security check fails for the
  358.    *        image, to some reasonable REJECT_* value.  This out param will only
  359.    *        be set if it's non-null.
  360.    * @return PR_TRUE if the load can proceed, or PR_FALSE if it is blocked.
  361.    *         Note that aImageBlockingStatus, if set will always be an ACCEPT
  362.    *         status if PR_TRUE is returned and always be a REJECT_* status if
  363.    *         PR_FALSE is returned.
  364.    */
  365.   static PRBool CanLoadImage(nsIURI* aURI,
  366.                              nsISupports* aContext,
  367.                              nsIDocument* aLoadingDocument,
  368.                              PRInt16* aImageBlockingStatus = nsnull);
  369.   /**
  370.    * Method to start an image load.  This does not do any security checks.
  371.    *
  372.    * @param aURI uri of the image to be loaded
  373.    * @param aLoadingDocument the document we belong to
  374.    * @param aObserver the observer for the image load
  375.    * @param aLoadFlags the load flags to use.  See nsIRequest
  376.    * @return the imgIRequest for the image load
  377.    */
  378.   static nsresult LoadImage(nsIURI* aURI,
  379.                             nsIDocument* aLoadingDocument,
  380.                             nsIURI* aReferrer,
  381.                             imgIDecoderObserver* aObserver,
  382.                             PRInt32 aLoadFlags,
  383.                             imgIRequest** aRequest);
  384.  
  385.   /**
  386.    * Method to get an nsIImage from an image loading content
  387.    *
  388.    * @param aContent The image loading content.  Must not be null.
  389.    * @param aRequest The image request [out]
  390.    * @return the nsIImage corresponding to the first frame of the image
  391.    */
  392.   static already_AddRefed<nsIImage> GetImageFromContent(nsIImageLoadingContent* aContent, imgIRequest **aRequest = nsnull);
  393.  
  394.   /**
  395.    * Method that decides whether a content node is draggable
  396.    *
  397.    * @param aContent The content node to test.
  398.    * @return whether it's draggable
  399.    */
  400.   static PRBool ContentIsDraggable(nsIContent* aContent) {
  401.     return IsDraggableImage(aContent) || IsDraggableLink(aContent);
  402.   }
  403.  
  404.   /**
  405.    * Method that decides whether a content node is a draggable image
  406.    *
  407.    * @param aContent The content node to test.
  408.    * @return whether it's a draggable image
  409.    */
  410.   static PRBool IsDraggableImage(nsIContent* aContent);
  411.  
  412.   /**
  413.    * Method that decides whether a content node is a draggable link
  414.    *
  415.    * @param aContent The content node to test.
  416.    * @return whether it's a draggable link
  417.    */
  418.   static PRBool IsDraggableLink(nsIContent* aContent);
  419.  
  420.   /**
  421.    * Method that gets the URI of the link content.  If the content
  422.    * isn't a link, return null.
  423.    *
  424.    * @param aContent The link content
  425.    * @return the URI the link points to
  426.    */
  427.   static already_AddRefed<nsIURI> GetLinkURI(nsIContent* aContent);
  428.  
  429.   /**
  430.    * Method that gets the XLink uri for a content node, if it's an XLink
  431.    *
  432.    * @param aContent The content node, possibly an XLink
  433.    * @return Null if aContent is not an XLink, the URI it points to otherwise
  434.    */
  435.   static already_AddRefed<nsIURI> GetXLinkURI(nsIContent* aContent);
  436.  
  437.   /**
  438.    * Convenience method to create a new nodeinfo that differs only by name
  439.    * from aNodeInfo.
  440.    */
  441.   static nsresult NameChanged(nsINodeInfo *aNodeInfo, nsIAtom *aName,
  442.                               nsINodeInfo** aResult)
  443.   {
  444.     nsNodeInfoManager *niMgr = aNodeInfo->NodeInfoManager();
  445.  
  446.     return niMgr->GetNodeInfo(aName, aNodeInfo->GetPrefixAtom(),
  447.                               aNodeInfo->NamespaceID(), aResult);
  448.   }
  449.  
  450.   /**
  451.    * Convenience method to create a new nodeinfo that differs only by prefix
  452.    * from aNodeInfo.
  453.    */
  454.   static nsresult PrefixChanged(nsINodeInfo *aNodeInfo, nsIAtom *aPrefix,
  455.                                 nsINodeInfo** aResult)
  456.   {
  457.     nsNodeInfoManager *niMgr = aNodeInfo->NodeInfoManager();
  458.  
  459.     return niMgr->GetNodeInfo(aNodeInfo->NameAtom(), aPrefix,
  460.                               aNodeInfo->NamespaceID(), aResult);
  461.   }
  462.  
  463.   /**
  464.    * Retrieve a pointer to the document that owns aNodeInfo.
  465.    */
  466.   static nsIDocument *GetDocument(nsINodeInfo *aNodeInfo)
  467.   {
  468.     return aNodeInfo->NodeInfoManager()->GetDocument();
  469.   }
  470.  
  471.   /**
  472.    * Returns the appropriate event argument name for the specified
  473.    * namespace.  Added because we need to switch between SVG's "evt"
  474.    * and the rest of the world's "event".
  475.    */
  476.   static const char *GetEventArgName(PRInt32 aNameSpaceID);
  477.  
  478.   /**
  479.    * Return the nsIXPConnect service.
  480.    */
  481.   static nsIXPConnect *XPConnect()
  482.   {
  483.     return sXPConnect;
  484.   }
  485.  
  486.   /**
  487.    * Report a localized error message to the error console.
  488.    *   @param aFile Properties file containing localized message.
  489.    *   @param aMessageName Name of localized message.
  490.    *   @param aParams Parameters to be substituted into localized message.
  491.    *   @param aParamsLength Length of aParams.
  492.    *   @param aURI URI of resource containing error (may be null).
  493.    *   @param aSourceLine The text of the line that contains the error (may be
  494.               empty).
  495.    *   @param aLineNumber Line number within resource containing error.
  496.    *   @param aColumnNumber Column number within resource containing error.
  497.    *   @param aErrorFlags See nsIScriptError.
  498.    *   @param aCategory Name of module reporting error.
  499.    */
  500.   enum PropertiesFile {
  501.     eCSS_PROPERTIES,
  502.     eXBL_PROPERTIES,
  503.     eXUL_PROPERTIES,
  504.     eLAYOUT_PROPERTIES,
  505.     eFORMS_PROPERTIES,
  506.     ePRINTING_PROPERTIES,
  507.     eDOM_PROPERTIES,
  508.     eBRAND_PROPERTIES,
  509.     PropertiesFile_COUNT
  510.   };
  511.   static nsresult ReportToConsole(PropertiesFile aFile,
  512.                                   const char *aMessageName,
  513.                                   const PRUnichar **aParams,
  514.                                   PRUint32 aParamsLength,
  515.                                   nsIURI* aURI,
  516.                                   const nsAFlatString& aSourceLine,
  517.                                   PRUint32 aLineNumber,
  518.                                   PRUint32 aColumnNumber,
  519.                                   PRUint32 aErrorFlags,
  520.                                   const char *aCategory);
  521.  
  522.   /**
  523.    * Get the localized string named |aKey| in properties file |aFile|.
  524.    */
  525.   static nsresult GetLocalizedString(PropertiesFile aFile,
  526.                                      const char* aKey,
  527.                                      nsXPIDLString& aResult);
  528.  
  529.   /**
  530.    * Fill (with the parameters given) the localized string named |aKey| in
  531.    * properties file |aFile|.
  532.    */
  533.   static nsresult FormatLocalizedString(PropertiesFile aFile,
  534.                                         const char* aKey,
  535.                                         const PRUnichar **aParams,
  536.                                         PRUint32 aParamsLength,
  537.                                         nsXPIDLString& aResult);
  538.  
  539.   /**
  540.    * Returns a list containing all elements in the document that are
  541.    * of type nsIContent::eHTML_FORM_CONTROL.
  542.    */
  543.   static already_AddRefed<nsContentList>
  544.   GetFormControlElements(nsIDocument *aDocument);
  545.  
  546.   /**
  547.    * Returns true if aDocument is a chrome document
  548.    */
  549.   static PRBool IsChromeDoc(nsIDocument *aDocument);
  550.  
  551.   /**
  552.    * Notify XPConnect if an exception is pending on aCx.
  553.    */
  554.   static void NotifyXPCIfExceptionPending(JSContext *aCx);
  555.  
  556.   /**
  557.    * Release *aSupportsPtr when the shutdown notification is received
  558.    */
  559.   static nsresult ReleasePtrOnShutdown(nsISupports** aSupportsPtr) {
  560.     NS_ASSERTION(aSupportsPtr, "Expect to crash!");
  561.     NS_ASSERTION(*aSupportsPtr, "Expect to crash!");
  562.     return sPtrsToPtrsToRelease->AppendElement(aSupportsPtr) ? NS_OK :
  563.       NS_ERROR_OUT_OF_MEMORY;
  564.   }
  565.   /**
  566.    * Make sure that whatever value *aPtr contains at any given moment is
  567.    * protected from JS GC until we remove the GC root.  A call to this that
  568.    * succeeds MUST be matched by a call to RemoveJSGCRoot to avoid leaking.
  569.    */
  570.   static nsresult AddJSGCRoot(jsval* aPtr, const char* aName) {
  571.     return AddJSGCRoot((void*)aPtr, aName);
  572.   }
  573.  
  574.   /**
  575.    * Make sure that whatever object *aPtr is pointing to at any given moment is
  576.    * protected from JS GC until we remove the GC root.  A call to this that
  577.    * succeeds MUST be matched by a call to RemoveJSGCRoot to avoid leaking.
  578.    */
  579.   static nsresult AddJSGCRoot(JSObject** aPtr, const char* aName) {
  580.     return AddJSGCRoot((void*)aPtr, aName);
  581.   }
  582.  
  583.   /**
  584.    * Make sure that whatever object *aPtr is pointing to at any given moment is
  585.    * protected from JS GC until we remove the GC root.  A call to this that
  586.    * succeeds MUST be matched by a call to RemoveJSGCRoot to avoid leaking.
  587.    */
  588.   static nsresult AddJSGCRoot(void* aPtr, const char* aName);  
  589.  
  590.   /**
  591.    * Remove aPtr as a JS GC root
  592.    */
  593.   static nsresult RemoveJSGCRoot(jsval* aPtr) {
  594.     return RemoveJSGCRoot((void*)aPtr);
  595.   }
  596.   static nsresult RemoveJSGCRoot(JSObject** aPtr) {
  597.     return RemoveJSGCRoot((void*)aPtr);
  598.   }
  599.   static nsresult RemoveJSGCRoot(void* aPtr);
  600.   
  601. private:
  602.   static nsresult doReparentContentWrapper(nsIContent *aChild,
  603.                                            JSContext *cx,
  604.                                            JSObject *aOldGlobal,
  605.                                            JSObject *parent_obj);
  606.  
  607.   static nsresult EnsureStringBundle(PropertiesFile aFile);
  608.  
  609.  
  610.   static nsIDOMScriptObjectFactory *sDOMScriptObjectFactory;
  611.  
  612.   static nsIXPConnect *sXPConnect;
  613.  
  614.   static nsIScriptSecurityManager *sSecurityManager;
  615.  
  616.   static nsIThreadJSContextStack *sThreadJSContextStack;
  617.  
  618.   static nsIParserService *sParserService;
  619.  
  620.   static nsINameSpaceManager *sNameSpaceManager;
  621.  
  622.   static nsIIOService *sIOService;
  623.  
  624. #ifdef MOZ_XTF
  625.   static nsIXTFService *sXTFService;
  626. #endif
  627.  
  628.   static nsIPrefBranch *sPrefBranch;
  629.  
  630.   static nsIPref *sPref;
  631.  
  632.   static imgILoader* sImgLoader;
  633.  
  634.   static nsIConsoleService* sConsoleService;
  635.  
  636.   static nsIStringBundleService* sStringBundleService;
  637.   static nsIStringBundle* sStringBundles[PropertiesFile_COUNT];
  638.  
  639.   // Holds pointers to nsISupports* that should be released at shutdown
  640.   static nsVoidArray* sPtrsToPtrsToRelease;
  641.  
  642.   // For now, we don't want to automatically clean this up in Shutdown(), since
  643.   // consumers might unfortunately end up wanting to use it after that
  644.   static nsIJSRuntimeService* sJSRuntimeService;
  645.   static JSRuntime* sScriptRuntime;
  646.   static PRInt32 sScriptRootCount;
  647.   
  648.   static PRBool sInitialized;
  649. };
  650.  
  651.  
  652. class nsCxPusher
  653. {
  654. public:
  655.   nsCxPusher(nsISupports *aCurrentTarget)
  656.     : mScriptIsRunning(PR_FALSE)
  657.   {
  658.     if (aCurrentTarget) {
  659.       Push(aCurrentTarget);
  660.     }
  661.   }
  662.  
  663.   ~nsCxPusher()
  664.   {
  665.     Pop();
  666.   }
  667.  
  668.   void Push(nsISupports *aCurrentTarget);
  669.   void Pop();
  670.  
  671. private:
  672.   nsCOMPtr<nsIJSContextStack> mStack;
  673.   nsCOMPtr<nsIScriptContext> mScx;
  674.   PRBool mScriptIsRunning;
  675. };
  676.  
  677. class nsAutoGCRoot {
  678. public:
  679.   // aPtr should be the pointer to the jsval we want to protect
  680.   nsAutoGCRoot(jsval* aPtr, nsresult* aResult) :
  681.     mPtr(aPtr)
  682.   {
  683.     mResult = *aResult =
  684.       nsContentUtils::AddJSGCRoot(aPtr, "nsAutoGCRoot");
  685.   }
  686.  
  687.   // aPtr should be the pointer to the JSObject* we want to protect
  688.   nsAutoGCRoot(JSObject** aPtr, nsresult* aResult) :
  689.     mPtr(aPtr)
  690.   {
  691.     mResult = *aResult =
  692.       nsContentUtils::AddJSGCRoot(aPtr, "nsAutoGCRoot");
  693.   }
  694.  
  695.   // aPtr should be the pointer to the thing we want to protect
  696.   nsAutoGCRoot(void* aPtr, nsresult* aResult) :
  697.     mPtr(aPtr)
  698.   {
  699.     mResult = *aResult =
  700.       nsContentUtils::AddJSGCRoot(aPtr, "nsAutoGCRoot");
  701.   }
  702.  
  703.   ~nsAutoGCRoot() {
  704.     if (NS_SUCCEEDED(mResult)) {
  705.       nsContentUtils::RemoveJSGCRoot(mPtr);
  706.     }
  707.   }
  708.  
  709. private:
  710.   void* mPtr;
  711.   nsresult mResult;
  712. };
  713.  
  714. #define NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(_class)                      \
  715.   if (aIID.Equals(NS_GET_IID(nsIClassInfo))) {                                \
  716.     foundInterface =                                                          \
  717.       nsContentUtils::GetClassInfoInstance(eDOMClassInfo_##_class##_id);      \
  718.     if (!foundInterface) {                                                    \
  719.       *aInstancePtr = nsnull;                                                 \
  720.       return NS_ERROR_OUT_OF_MEMORY;                                          \
  721.     }                                                                         \
  722.   } else
  723.  
  724. #define NS_INTERFACE_MAP_ENTRY_TEAROFF(_interface, _allocator)                \
  725.   if (aIID.Equals(NS_GET_IID(_interface))) {                                  \
  726.     foundInterface = NS_STATIC_CAST(_interface *, _allocator);                \
  727.     if (!foundInterface) {                                                    \
  728.       *aInstancePtr = nsnull;                                                 \
  729.       return NS_ERROR_OUT_OF_MEMORY;                                          \
  730.     }                                                                         \
  731.   } else
  732.  
  733. #endif /* nsContentUtils_h___ */
  734.